<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="物料号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialCode">
              <a-input v-model="model.materialCode" :placeholder="materialCodePlaceholder"  :disabled="materialCodeDisabled"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
              <a-input v-model="model.materialName" placeholder="请输入物料名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialTypeId">
              <j-search-select-tag v-model="model.materialTypeId" dict="mdm_material_type,material_type_name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="物料分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialClassId">
              <j-tree-select
                v-model="model.materialClassId"
                dict="mdm_material_class,material_class_name,id"
                pid-field="pid">
              </j-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="物料来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialSourceCode">
              <j-dict-select-tag type="list" v-model="model.materialSourceCode" dictCode="mdm_material_source" placeholder="请选择物料来源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialUnitCode">
              <j-search-select-tag type="list" v-model="model.materialUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialSpec">
              <a-input v-model="model.materialSpec" placeholder="请输入规格"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialMaterial">
              <a-input v-model="model.materialMaterial" placeholder="请输入材质"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="生产厂家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialFactory">
              <a-input v-model="model.materialFactory" placeholder="请输入生产厂家"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="属性组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="propGroupId">
              <j-dict-select-tag type="list" v-model="model.propGroupId" dictCode="mdm_material_prop_group,name,id" placeholder="请选择属性组" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="属性" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="materialProp_dictText">
              <div class="components-input-demo-presuffix">
                <a-input  v-model="model.materialProp_dictText"
                          @click="onSearchMaterialProp"
                          placeholder="请选择属性"
                          readOnly
                          unselectable="on">
                  <a-icon slot="prefix" type="tags"/>
                  <a-icon slot="suffix" type="close-circle" @click="handleMaterialPropEmpty" title="清空"/>
                </a-input>
              </div>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="图号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialGraphNo">
              <a-input v-model="model.materialGraphNo" placeholder="请输入图号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="产地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialOriginPlace">
              <a-input v-model="model.materialOriginPlace" placeholder="请输入产地"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="旧物料号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oldMaterialCode">
              <a-input v-model="model.oldMaterialCode" placeholder="请输入旧物料号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="启用标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableFlag">
              <j-switch v-model="model.enableFlag" :options="[1,0]"></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialBrand">
              <a-input v-model="model.materialBrand" placeholder="请输入品牌"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="宽度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialWidth">
              <a-input-number v-model="model.materialWidth" placeholder="请输入宽度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="厚度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialThickness">
              <a-input-number v-model="model.materialThickness" placeholder="请输入厚度" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="旧物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oldMaterialName">
              <a-input v-model="model.oldMaterialName" placeholder="请输入旧物料名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="辅助单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auxiliaryUnitCode">
              <j-search-select-tag type="list" v-model="model.auxiliaryUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择辅助单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialLength">
              <a-input-number v-model="model.materialLength" placeholder="请输入长度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="净重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialNetWeight">
              <a-input-number v-model="model.materialNetWeight" placeholder="请输入净重" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="颜色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialColor">
              <a-input v-model="model.materialColor" placeholder="请输入颜色"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="毛重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialGrossWeight">
              <a-input-number v-model="model.materialGrossWeight" placeholder="请输入毛重" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="重量单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weighUnitCode">
              <j-search-select-tag type="list" v-model="model.weighUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择重量单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="面积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialArea">
              <a-input-number v-model="model.materialArea" placeholder="请输入面积" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="体积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialVolume">
              <a-input-number v-model="model.materialVolume" placeholder="请输入体积" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="体积单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="volumeUnitCode">
              <j-search-select-tag type="list" v-model="model.volumeUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择体积单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="物料状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialStatusCode">
              <a-input v-model="model.materialStatusCode" placeholder="请输入物料状态"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="销售标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialSaleFlag">
              <j-switch v-model="model.materialSaleFlag" :options="[1,0]"></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="出入库策略" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inoutStrategyCode">
              <a-input v-model="model.inoutStrategyCode" placeholder="请输入出入库策略"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="生产仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionStockId">
              <j-search-select-tag v-model="model.productionStockId" dict="mdm_warehouse,warehouse_name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="采购仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buyStockId">
              <j-search-select-tag v-model="model.buyStockId" dict="mdm_warehouse,warehouse_name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="生效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialEffectiveDate">
              <j-date placeholder="请选择生效日期" v-model="model.materialEffectiveDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="失效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialExpiryDate">
              <j-date placeholder="请选择失效日期" v-model="model.materialExpiryDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="采购组织" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purDeptId">
              <a-input v-model="model.purDeptId" placeholder="请输入采购组织"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="采购员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialBuyerId">
              <j-select-user-by-dep v-model="model.materialBuyerId" :multi="false"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="批次标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialBatchFlag">
              <j-switch v-model="model.materialBatchFlag" :options="[1,0]"></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="自动采购标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialBuyFlag">
              <j-switch v-model="model.materialBuyFlag" :options="[1,0]"></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="ABC标识" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialAbcFlag">
              <a-input v-model="model.materialAbcFlag" placeholder="请输入ABC标识"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="计划员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialPlannerId">
              <j-select-user-by-dep v-model="model.materialPlannerId" :multi="false"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="批量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="batchTypeId">
              <a-input v-model="model.batchTypeId" placeholder="请输入批量"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="最小批量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialMinimum">
              <a-input-number v-model="model.materialMinimum" placeholder="请输入最小批量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="最大批量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialMaximum">
              <a-input-number v-model="model.materialMaximum" placeholder="请输入最大批量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="固定批量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialFixed">
              <a-input-number v-model="model.materialFixed" placeholder="请输入固定批量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="安全库存" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialSafeStock">
              <a-input-number v-model="model.materialSafeStock" placeholder="请输入安全库存" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="反冲标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialBackFlag">
              <j-switch v-model="model.materialBackFlag" :options="[1,0]"></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="自制生产（天）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionDays">
              <a-input-number v-model="model.productionDays" placeholder="请输入自制生产（天）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="计划交货时间（天）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deliveryDays">
              <a-input-number v-model="model.deliveryDays" placeholder="请输入计划交货时间（天）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="收货处理时间（天）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveDays">
              <a-input-number v-model="model.receiveDays" placeholder="请输入收货处理时间（天）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="单位换算定义" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitConversionId">
              <a-input v-model="model.unitConversionId" placeholder="请输入单位换算定义"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="换算主单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="conversionMainUnit">
              <j-search-select-tag type="list" v-model="model.conversionMainUnit" dict="mdm_unit,unit_name,unit_code" placeholder="请选择换算主单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="换算辅单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="conversionSubUnit">
              <j-search-select-tag type="list" v-model="model.conversionSubUnit" dict="mdm_unit,unit_name,unit_code" placeholder="请选择换算辅单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="换算率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="conversionRate">
              <a-input-number v-model="model.conversionRate" placeholder="请输入换算率" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="默认采购单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buyUnitCode">
              <j-search-select-tag type="list" v-model="model.buyUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择默认采购单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="默认销售单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saleUnitCode">
              <j-search-select-tag type="list" v-model="model.saleUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择默认销售单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="默认库存单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stockUnitCode">
              <j-search-select-tag type="list" v-model="model.stockUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择默认库存单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" :hidden="colHidden">
            <a-form-model-item label="默认发料单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="issueUnitCode">
              <j-search-select-tag type="list" v-model="model.issueUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择默认发料单位" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="23" :style="{textAlign: 'right'}">
            <a :style="{ marginLeft: '8px', fontSize: '14px' }" @click="moreInfo">
              更多信息 <a-icon :type="colHidden ? 'down' : 'up'" />
            </a>
          </a-col>
        </a-row>
      </a-form-model>
      <yq-popup-material-prop-sel ref="materialPropSel"  @ok="materialPropPopupCallback" :prop-group-id="this.model.propGroupId" :material-prop-val-ids="this.model.materialProp"/>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="工厂视图" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="mdmMaterialFactoryDetailTable.loading"
          :columns="mdmMaterialFactoryDetailTable.columns"
          :dataSource="mdmMaterialFactoryDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="财务视图" :key="refKeys[1]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[1]"
          :loading="mdmMaterialFinanceDetailTable.loading"
          :columns="mdmMaterialFinanceDetailTable.columns"
          :dataSource="mdmMaterialFinanceDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="计量单位" :key="refKeys[2]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[2]"
          :loading="mdmMaterialUnitDetailTable.loading"
          :columns="mdmMaterialUnitDetailTable.columns"
          :dataSource="mdmMaterialUnitDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="图片" key="pic" :forceRender="true">
        <a-card :bordered="false" :body-style="{padding: '0px 3px 25px 3px'}" >
          <j-upload v-model="model.materialPic" text="上传" fileType="image"></j-upload>
        </a-card>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

import { getAction } from '@/api/manage'
import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
import {cloneObject, validateDuplicateValue} from '@/utils/util'
import YqPopupMaterialPropSel from '@/views/slf/components/YqPopupMaterialPropSel'

export default {
  name: 'MdmMaterialForm',
  mixins: [JEditableTableModelMixin],
  components: {
    YqPopupMaterialPropSel
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      colHidden: true,
      model:{
        enableFlag: 1
      },
      materialCodeDisabled: false,
      materialCodePlaceholder: '请输入物料号',
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {
        materialName: [
          { required: true, message: '请输入物料名称!'},
        ],
        materialTypeId: [
          { required: true, message: '请输入物料类型!'},
        ],
        materialSourceCode: [
          { required: true, message: '请输入物料来源!'},
        ],
        materialUnitCode: [
          { required: true, message: '请输入单位!'},
        ],
      },
      refKeys: ['mdmMaterialFactoryDetail', 'mdmMaterialFinanceDetail', 'mdmMaterialUnitDetail', ],
      tableKeys:['mdmMaterialFactoryDetail', 'mdmMaterialFinanceDetail', 'mdmMaterialUnitDetail', ],
      activeKey: 'mdmMaterialFactoryDetail',
      // 工厂视图
      mdmMaterialFactoryDetailTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '工厂',
            key: 'factoryId',
            type: FormTypes.select,
            dictCode:"mdm_factory,factory_name,id",
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: 'MRP类型',
            key: 'mrpType',
            type: FormTypes.select,
            dictCode:"mdm_mrp_type",
            width:"130px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '采购类型',
            key: 'buyType',
            type: FormTypes.select,
            dictCode:"mdm_buy_type",
            width:"130px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '生产天数',
            key: 'productionDays',
            type: FormTypes.inputNumber,
            width:"100px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '采购天数',
            key: 'buyDays',
            type: FormTypes.inputNumber,
            width:"100px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '按单模式',
            key: 'orderFlag',
            type: FormTypes.select,
            dictCode:"yn",
            width:"100px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '库存类型',
            key: 'stockType',
            type: FormTypes.select,
            dictCode:"mdm_stock_type",
            width:"130px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '批次管理',
            key: 'batchFlag',
            type: FormTypes.select,
            dictCode:"yn",
            width:"100px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '序列号管理',
            key: 'seqFlag',
            type: FormTypes.select,
            dictCode:"yn",
            width:"100px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '重订货点',
            key: 'reorderPlace',
            type: FormTypes.input,
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '最大库存',
            key: 'maxStock',
            type: FormTypes.inputNumber,
            width:"100px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '采购组',
            key: 'buyGroup',
            type: FormTypes.select,
            dictCode:"mdm_buy_group",
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: 'MRP控制员',
            key: 'mrpManager',
            type: FormTypes.select,
            dictCode:"mdm_mrp_manager",
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '生产管理员',
            key: 'productionManager',
            type: FormTypes.select,
            dictCode:"mdm_mrp_manager",
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '固定仓位',
            key: 'stockPlace',
            type: FormTypes.input,
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '采购仓库',
            key: 'buyStockId',
            type: FormTypes.input,
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },

        ]
      },
      // 财务视图
      mdmMaterialFinanceDetailTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '工厂',
            key: 'factoryId',
            type: FormTypes.select,
            dictCode:"mdm_factory,factory_name,id",
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '评估类',
            key: 'assessType',
            type: FormTypes.select,
            dictCode:"mdm_assess_type",
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '价格控制',
            key: 'priceType',
            type: FormTypes.select,
            dictCode:"mdm_price_type",
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '移动平均价',
            key: 'movingAveragePrice',
            type: FormTypes.inputNumber,
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '标准价',
            key: 'standardPrice',
            type: FormTypes.inputNumber,
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
        ]
      },
      // 计量单位
      mdmMaterialUnitDetailTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '可选单位',
            key: 'auxiliaryUnitCode',
            type: FormTypes.select,
            dictCode:"unit",
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
          {
            title: '转换率',
            key: 'conversionRate',
            type: FormTypes.inputNumber,
            width:"200px",
            placeholder: '请输入${title}',
            defaultValue:'',
          },
        ]
      },
      url: {
        add: "/mdm/material/add",
        edit: "/mdm/material/editMain",
        queryById: "/mdm/material/queryById",
        genMaterialCode: "/mdm/material/genMaterialCode",
        checkMaterialCodeGenerate: "/mdm/material/checkMaterialCodeGenerate",
        mdmMaterialFactoryDetail: {
          list: '/mdm/material/queryMdmMaterialFactoryDetailByMainId'
        },
        mdmMaterialFinanceDetail: {
          list: '/mdm/material/queryMdmMaterialFinanceDetailByMainId'
        },
        mdmMaterialUnitDetail: {
          list: '/mdm/material/queryMdmMaterialUnitDetailByMainId'
        },
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    this.checkMaterialCodeGen();
  },
  methods: {
    addBefore(){
      this.mdmMaterialFactoryDetailTable.dataSource=[]
      this.mdmMaterialFinanceDetailTable.dataSource=[]
      this.mdmMaterialUnitDetailTable.dataSource=[]
    },
    getAllTable() {
      let values = this.tableKeys.map(key => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter() {
      this.$nextTick(() => {
      })
      // 加载子表数据
      if (this.model.id) {
        let params = { id: this.model.id }
        this.requestSubTableData(this.url.mdmMaterialFactoryDetail.list, params, this.mdmMaterialFactoryDetailTable)
        this.requestSubTableData(this.url.mdmMaterialFinanceDetail.list, params, this.mdmMaterialFinanceDetailTable)
        this.requestSubTableData(this.url.mdmMaterialUnitDetail.list, params, this.mdmMaterialUnitDetailTable)
      }
    },
    //校验所有一对一子表表单
    validateSubForm(allValues){
      return new Promise((resolve,reject)=>{
        Promise.all([
        ]).then(() => {
          resolve(allValues)
        }).catch(e => {
          if (e.error === VALIDATE_NO_PASSED) {
            // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
            this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
          } else {
            console.error(e)
          }
        })
      })
    },
    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        mdmMaterialFactoryDetailList: allValues.tablesValue[0].values,
        mdmMaterialFinanceDetailList: allValues.tablesValue[1].values,
        mdmMaterialUnitDetailList: allValues.tablesValue[2].values,
      }
    },
    validateError(msg){
      this.$message.error(msg)
    },
    moreInfo(){
      this.colHidden = !this.colHidden;
    },
    onSearchMaterialProp(){
      if(!this.model.propGroupId){
        this.$message.warning('请先选择属性组！');
        return;
      }
      this.$refs.materialPropSel.show();
    },
    handleMaterialPropEmpty(){
      this.model.materialProp = ''
      this.model.materialProp_dictText = ''
    },
    materialPropPopupCallback(selectedPropData){
      this.$set(this.model,'materialProp', selectedPropData.value)
      this.$set(this.model,'materialProp_dictText', selectedPropData.text)
    },
    checkMaterialCodeGen(){
      let that = this;
      getAction(that.url.checkMaterialCodeGenerate, {}).then((res) => {
        if (res.success) {
          this.materialCodeDisabled = res.result
          if(this.materialCodeDisabled){
            this.materialCodePlaceholder = '物料号由系统自动生成';
          }else{
            this.materialCodePlaceholder = '请输入物料号';
            this.$set(this.validatorRules, 'materialCode',[{ required: true, message: '请输入物料号!'},] );
          }
        } else {
          that.$message.warning(res.message);
        }
      })
    },
    copy(record) {
      if(record && '{}'!=JSON.stringify(record)){
        this.tableReset();
      }
      let copy = cloneObject(record);
      copy.id = null;
      copy.materialCode = null;
      copy.materialPic = null;
      this.visible = true
      this.activeKey = this.refKeys[0]
      setTimeout(()=>{
        this.$refs.form.resetFields()
        this.model = Object.assign({}, copy)
        if (typeof this.copyAfter === 'function') this.copyAfter(record.id)
      },500)
    },
    copyAfter(id) {
      // 加载子表数据
      if (id) {
        let params = { id: id }
        this.copySubTableData(this.url.mdmMaterialFactoryDetail.list, params, this.mdmMaterialFactoryDetailTable)
        this.copySubTableData(this.url.mdmMaterialFinanceDetail.list, params, this.mdmMaterialFinanceDetailTable)
        this.copySubTableData(this.url.mdmMaterialUnitDetail.list, params, this.mdmMaterialUnitDetailTable)
      }
    },
    copySubTableData(url, params, tab, success) {
      tab.loading = true
      getAction(url, params).then(res => {
        let { result } = res
        let dataSource = []
        if (result) {
          if (Array.isArray(result)) {
            dataSource = result
          } else if (Array.isArray(result.records)) {
            dataSource = result.records
          }
        }
        dataSource.forEach(data => data.id = null);
        tab.dataSource = dataSource
        typeof success === 'function' ? success(res) : ''
      }).finally(() => {
        tab.loading = false
      })
    },
  }
}
</script>

<style scoped>
.action-button {
  margin-bottom: 8px;
}
.gap {
  padding-left: 8px;
}
.components-input-demo-presuffix .anticon-close-circle {
  cursor: pointer;
  color: #ccc;
  transition: color 0.3s;
  font-size: 12px;
}

.components-input-demo-presuffix .anticon-close-circle:hover {
  color: #f5222d;
}

.components-input-demo-presuffix .anticon-close-circle:active {
  color: #666;
}
</style>